package org.example.ims.Controller.Order;

import org.example.ims.Entity.Order.InStockDetail;
import org.example.ims.Service.Order.InStockDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/inStockDetails")
public class InStockDetailController {

    private final InStockDetailService inStockDetailService;

    @Autowired
    public InStockDetailController(InStockDetailService inStockDetailService) {
        this.inStockDetailService = inStockDetailService;
    }

    @PostMapping
    public ResponseEntity<InStockDetail> createDetail(@RequestBody InStockDetail detail) {
        InStockDetail createdDetail = inStockDetailService.createDetail(detail);
        return ResponseEntity.ok(createdDetail);
    }

    @PutMapping("/{detailId}")
    public ResponseEntity<InStockDetail> updateDetail(@PathVariable Integer detailId, @RequestBody InStockDetail detail) {
        InStockDetail updatedDetail = inStockDetailService.updateDetail(detailId, detail);
        return ResponseEntity.ok(updatedDetail);
    }

    @DeleteMapping("/{detailId}")
    public ResponseEntity<Void> deleteDetail(@PathVariable Integer detailId) {
        inStockDetailService.deleteDetail(detailId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{detailId}")
    public ResponseEntity<InStockDetail> getDetailById(@PathVariable Integer detailId) {
        InStockDetail detail = inStockDetailService.getDetailById(detailId);
        return ResponseEntity.ok(detail);
    }

    @GetMapping("/search")
    public ResponseEntity<Page<InStockDetail>> getAllDetails(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<InStockDetail> details = inStockDetailService.getAllDetails(page, size);
        return ResponseEntity.ok(details);
    }

    @GetMapping
    public ResponseEntity<Page<InStockDetail>> searchDetails(
            @RequestParam(required = false) Integer orderId,
            @RequestParam(required = false) Integer productId,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<InStockDetail> details = inStockDetailService.searchDetails(orderId, productId, page, size);
        return ResponseEntity.ok(details);
    }
}